package com.chenfan.process.entity.po;

import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.ccp.common.exception.ApiException;
import com.chenfan.ccp.plug.mybatis.annotation.Column;
import com.chenfan.ccp.plug.mybatis.annotation.Id;
import com.chenfan.ccp.plug.mybatis.handler.DbHandler;
import com.chenfan.ccp.util.tool.UuidUtil;
import com.chenfan.infra.context.UserContextHolder;
import com.chenfan.process.common.exception.ProcessResultCode;
import com.chenfan.process.entity.dto.ApprovalTemplateReceivedDTO;
import com.chenfan.process.entity.vo.ApprovalTemplateVo;
import com.chenfan.process.enums.BooleanEnum;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @author K
 * @date 2021/1/26
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@TableName(value = "chenfan_paas_approval_template_po")
public class ApprovalTemplatePo {

    @Id
    @JsonSerialize(using = ToStringSerializer.class)
    protected Long id;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(dateGeneStrategy = DbHandler.DateGeneStrategy.CREATE)
    protected Date createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(dateGeneStrategy = DbHandler.DateGeneStrategy.UPDATE)
    protected Date updateTime;
    @Column(commit = "物理删除开关 true 删除 false 不删除 默认为false", defaultVal = "false")
    protected Boolean deletedFlag;
    @Column(commit = "模板编码")
    private String code;

    @Column(commit = "审批模板标题")
    private String title;

    @Column(commit = "所在分组")
    private String templateGroup;

    @Column(commit = "表单说明")
    private String description;

    @Column(commit = "状态", defaultVal = "0")
    private Integer status;

    @Column(commit = "创建人")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long createBy;

    @Column(commit = "创建人名称")
    private String createName;

    @Column(commit = "更新人")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long updateBy;

    @Column(commit = "更新人名称")
    private String updateName;

    @Column(commit = "审批模板当前版本")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long version;

    /**
     * 审批快照id
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private Long approvalSnapshotId;

    /**
     * 是否检查待办生成
     */
    private Integer notifyFlag;

    /**
     * 审批过程中是否自动发消息/待办
     */
    @Deprecated
    private Integer autoNotifyFlag;
    /**
     * 租户id
     */
    private Long tenantId;

    /**
     * 是否支持转移, 1-支持 0-不支持
     */
    @Deprecated
    private Integer transferFlag;
    /**
     * 所属系统id
     */
    private Long belongId;

    /**
     * 所属系统
     */
    private String belongSystem;

    /**
     * 所属系统名称
     */
    private String belongSystemName;


    @Data
    public static class Constants {
        public static final String TITLE = "title";
        public static final String CODE = "code";
        public static final String TRANSFER_FLAG = "transfer_flag";

        private Constants() {}
    }

    public boolean needNotify() {
        return BooleanEnum.Y.getCode().equals(notifyFlag);
    }

    public ApprovalTemplateVo copyApprovalTemplateVo() {
        ApprovalTemplateVo vo = new ApprovalTemplateVo();
        org.springframework.beans.BeanUtils.copyProperties(this, vo);
        return vo;
    }

    public static ApprovalTemplatePo of(ApprovalTemplateReceivedDTO template, boolean create) {
        ApprovalTemplatePo approvalTemplatePo = new ApprovalTemplatePo();
        if (create) {
            Long id = UuidUtil.generateId();
            approvalTemplatePo.setId(id);
            approvalTemplatePo.setCode(id.toString());
            approvalTemplatePo.setCreateBy(UserContextHolder.getUserVo().getUserId());
            approvalTemplatePo.setCreateName(UserContextHolder.getUserVo().getRealName());
        } else {
            approvalTemplatePo.setUpdateBy(UserContextHolder.getUserVo().getUserId());
            approvalTemplatePo.setUpdateName(UserContextHolder.getUserVo().getRealName());
        }
        approvalTemplatePo.setTitle(template.getTitle());
        approvalTemplatePo.setDescription(template.getTitle());
        approvalTemplatePo.setTemplateGroup(template.getTemplateGroup());
        if (!StringUtils.isEmpty(template.getCode())) {
            approvalTemplatePo.setCode(template.getCode());
        }
//        approvalTemplatePo.setApprovalNotify(template.getApprovalNotify());
//        approvalTemplatePo.setRefuseNotify(template.getRefuseNotify());
//        approvalTemplatePo.setPassNotify(template.getPassNotify());
//        approvalTemplatePo.setCcNotify(template.getCcNotify());
//        approvalTemplatePo.setRemindNotify(template.getRemindNotify());
//        approvalTemplatePo.setOverDueNotify(template.getOverDueNotify());
        approvalTemplatePo.setBelongId(template.getBelongId());
        approvalTemplatePo.setBelongSystem(template.getBelongSystem());
        approvalTemplatePo.setBelongSystemName(template.getBelongSystemName());
        return approvalTemplatePo;
    }

    public void availableCheck() {
        if (BooleanEnum.Y.getCode().equals(status)) {
            throw new ApiException(ProcessResultCode.APPROVAL_TEMPLATE_DISABLED);
        }
    }
}
